<?php
namespace Home\Biz;

class SkuBiz{
	
	public static function searchSkuList($sku_en){
		
		// 参数检查start
		if(empty($sku_en['page_index'])){
			$sku_en['page_index'] = 1;
		}
		
		if(empty($sku_en['page_index'])){
			$sku_en['page_size'] = 20;
		}
		// 参数检查end
		
		
		// 搜索条件start
		$condition_array = array();
		if(!empty($sku_en['style_sn'])){
			$condition_array['style_sn'] = array('like', "%".$sku_en['style_sn']."%");
		}
		
		if(!empty($sku_en['style_name'])){
			$condition_array['style_name'] = array('like', "%".$sku_en['style_name']."%");
		}
		
		switch ($sku_en['is_buy_fz']) {
			case '0':
				$condition_array['is_buy_fz'] = array('eq', $sku_en['is_buy_fz']);
				break;
			case '1':
				$condition_array['is_buy_fz'] = array('eq', $sku_en['is_buy_fz']);
				break;
		}
		// 搜索条件end
		
		$return_en = array();
		$total_count = M('sku','','DB_trade')
		->where($condition_array)
		->order('create_date DESC')
		->count();
		$return_en['total_count'] = $total_count;
		
		
		$lst_sku_en = M('sku','','DB_trade')
		->page($sku_en['page_index'], $sku_en['page_size'])
		->order('create_date DESC')
		->where($condition_array)
		->select();
		
		
		foreach($lst_sku_en as $key => $en){
			$lst_sku_en[$key]['is_buy_fz_text'] = StringBiz::getSkuIsBuyFzText($en['is_buy_fz']);
			$lst_sku_en[$key]['status_text'] = StringBiz::getSkuStatusText($en['status']);
		}
		
		$return_en['lst_sku_en'] = $lst_sku_en;
	
		$total_page = 0;
		$rem = $total_count % $sku_en['page_size'];
		if($rem == 0){
			$total_page = $total_count / $sku_en['page_size'];
		}
		else{
			$total_page = floor($total_count / $sku_en['page_size']) + 1;
		}
		$return_en['total_page'] = $total_page;
		
		return $return_en;
	}
	
	
	public static function updateSku($sku_id ,$sku_sn,$sku_name,$taobao_sku_id,$taobao_sku_properties_name ,$style_sn,
			$style_name,$promotion_text,$taobao_style_id ,$taobao_style_name,$taobao_category_id ,
			$sku_dimension_id1 ,$sku_dimension_name1 ,$sku_dimension_value1 ,$sku_dimension_id2,
			$sku_dimension_name2,$sku_dimension_value2 ,$market_price ,$sale_price,$person_commission_amount,
			$dealer_commission_amount,$image,$image_list ,$material_no ,$material_desc,$code ,
			$haier_material_no,$material_length ,$material_width,$material_height ,$material_volume ,
			$material_weight ,$is_buy_fz,$status ,$update_date){
	
			$update_sku_en = array(
					'sku_sn'	=> $sku_sn,
					'sku_name'	=> $sku_name,
					'taobao_sku_id'	=> $taobao_sku_id,
					'taobao_sku_properties_name'	=> $taobao_sku_properties_name ,
					'style_sn'	=> $style_sn,
					'style_name'	=> $style_name,
					'promotion_text'	=> $promotion_text,
					'taobao_style_id'	=> $taobao_style_id ,
					'taobao_style_name'	=> $taobao_style_name,
					'taobao_category_id'	=> $taobao_category_id ,
					'sku_dimension_id1'	=> $sku_dimension_id1 ,
					'sku_dimension_name1'	=> $sku_dimension_name1 ,
					'sku_dimension_value1'	=> $sku_dimension_value1 ,
					'sku_dimension_id2'	=> $sku_dimension_id2,
					'sku_dimension_name2'	=> $sku_dimension_name2,
					'sku_dimension_value2'	=> $sku_dimension_value2 ,
					'market_price'	=> $market_price ,
					'sale_price'	=> $sale_price,
					'person_commission_amount'	=> $person_commission_amount,
					'dealer_commission_amount'	=> $dealer_commission_amount,
					'image'	=> $image,
					'image_list'	=> $image_list,
					'material_no'	=> $material_no ,
					'material_desc'	=> $material_desc,
					'69_code'	=> $code ,
					'haier_material_no'	=> $haier_material_no,
					'material_length'	=> $material_length ,
					'material_width'	=> $material_width,
					'material_height'	=> $material_height ,
					'material_volume'	=> $material_volume ,
					'material_weight'	=> $material_weight ,
					'is_buy_fz'	=> $is_buy_fz,
					'status'	=> $status ,
					'update_date'	=> date("y-m-d H:i:s", time())
			);
			M('sku','','DB_trade')->where("sku_id ='%s'", $sku_id)->save($update_sku_en);
	}
	public static function addSku($sku_sn,$sku_name,$taobao_sku_id,$taobao_sku_properties_name ,$style_sn,
			$style_name,$promotion_text,$taobao_style_id ,$taobao_style_name,$taobao_category_id ,
			$sku_dimension_id1 ,$sku_dimension_name1 ,$sku_dimension_value1 ,$sku_dimension_id2,
			$sku_dimension_name2,$sku_dimension_value2 ,$market_price ,$sale_price,$person_commission_amount,
			$dealer_commission_amount,$image,$image_list ,$material_no ,$material_desc,$code ,
			$haier_material_no,$material_length ,$material_width,$material_height ,$material_volume ,
			$material_weight ,$is_buy_fz,$status){
				$sku_id = SkuBiz::generateSkuId();
				$add_sku_en = array(
						'sku_id'	=>$sku_id,
						'sku_sn'	=> $sku_sn,
						'sku_name'	=> $sku_name,
						'taobao_sku_id'	=> $taobao_sku_id,
						'taobao_sku_properties_name'	=> $taobao_sku_properties_name ,
						'style_sn'	=> $style_sn,
						'style_name'	=> $style_name,
						'promotion_text'	=> $promotion_text,
						'taobao_style_id'	=> $taobao_style_id ,
						'taobao_style_name'	=> $taobao_style_name,
						'taobao_category_id'	=> $taobao_category_id ,
						'sku_dimension_id1'	=> $sku_dimension_id1 ,
						'sku_dimension_name1'	=> $sku_dimension_name1 ,
						'sku_dimension_value1'	=> $sku_dimension_value1 ,
						'sku_dimension_id2'	=> $sku_dimension_id2,
						'sku_dimension_name2'	=> $sku_dimension_name2,
						'sku_dimension_value2'	=> $sku_dimension_value2 ,
						'market_price'	=> $market_price ,
						'sale_price'	=> $sale_price,
						'person_commission_amount'	=> $person_commission_amount,
						'dealer_commission_amount'	=> $dealer_commission_amount,
						'image'	=> $image,
						'image_list'	=> $image_list,
						'material_no'	=> $material_no ,
						'material_desc'	=> $material_desc,
						'69_code'	=> $code ,
						'haier_material_no'	=> $haier_material_no,
						'material_length'	=> $material_length ,
						'material_width'	=> $material_width,
						'material_height'	=> $material_height ,
						'material_volume'	=> $material_volume ,
						'material_weight'	=> $material_weight ,
						'is_buy_fz'	=> $is_buy_fz,
						'status'	=> $status ,
						'create_user'	=> "whaleyFz.admin skuBiz.addSku",
						'create_date'	=> date("y-m-d H:i:s", time())
				);
				M('sku','','DB_trade')->add($add_sku_en);
	}
	public static function generateSkuId(){
		$seq = 10000000 + CommonBiz::getNewSeq('sku_id');
		return 'SK'.$seq;
	}
}